package com.cjst.controller.base;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.controller.base.BaseController;
import com.cjst.pojo.base.Article;
import com.cjst.pojo.part.Parts;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.MapUtils;
import com.cjst.service.base.ArticleService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.CloneClass;
import com.cjst.util.code.PageUtil;
import com.cjst.util.common.DateUtil;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController{
	protected final Logger log = Logger.getLogger(ArticleController.class);
	@Autowired
	private ArticleService articleService;
	@RequestMapping("/toAddArticle")
	public String toAddArticle(Model model){
		return "article/article_list";
	}
	
	@RequestMapping("add")
	public String add(){
		return "article/toAddArticle";
	}
	@RequestMapping("edit/{id}")
	public String edit(@PathVariable String id, Model model){
		WherePrams where = Method.where("id", C.EQ, id);
        Article article = articleService.get(where);
        if (article != null) {
            Map<String, Object> articleMap = MapUtils.transBean2Map(article);
            model.addAttribute("article", articleMap);
        }
		
		return "article/edit";
	}
	@RequestMapping("detail/{id}")
	public String detail(@PathVariable String id, Model model){
		 WherePrams where = Method.where("id", C.EQ, id);
	        Article article = articleService.get(where);
	        if (article != null) {
	            Map<String, Object> articleMap = MapUtils.transBean2Map(article);
	            model.addAttribute("article", articleMap);
	        }
		
		return "article/detail";
	}
	
	@RequestMapping("/addArticle")
	public String addArticle(String content,Integer type,Integer isTop,String title,String contentText,String textSource,String topUrl,String num){
		//System.out.println("===============>>"+content);
		log.info("===============>>"+content);
		Article arc=new Article();
		arc.setType(type);
		arc.setContentText(contentText);
		arc.setContent(content);
		arc.setTitle(title);
		arc.setTextSource(textSource);
		arc.setIsTop(isTop);
		arc.setNum(num);
		arc.setTopUrl(topUrl);
		arc.setCreateTime(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
		arc.setPx(new Date().getTime());
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		arc.setCreateUser(sysUser.getId());
		arc.setCreateName(sysUser.getName());
		articleService.addLocalByid(arc);
		return "article/toAddArticle";
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public Object list(){
		WherePrams wherePrams=Method.where("isDelete",C.EQ,0);
		
		PageUtil<Article> pageUtil = articleService.listPageCount(wherePrams);
        List<Article> articles = pageUtil.getData();
		
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Article article : articles) {
            Map<String, Object> articleMap = MapUtils.transBean2Map(article);
            maps.add(articleMap);
        }
        
        PageUtil<Map<String, Object>> pages = new PageUtil<>();

        CloneClass.getClone(pageUtil, pages);
        pages.setData(maps);
        return this.packForBT(pages);
	}
	
	@RequestMapping(value = "/save/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(String id,String content,Integer type,Integer isTop,String title,String contentText,String textSource,String topUrl,String num){
		WherePrams wherePrams=Method.where("isDelete",C.EQ,0);
		wherePrams.and("id", C.EQ, id);
		
		Article arc=articleService.get(wherePrams);
		if(arc==null) {
			return new ResultObj(0, "error", 0);
		}
		arc.setType(type);
		arc.setContentText(contentText);
		arc.setContent(content);
		arc.setNum(num);
		arc.setTopUrl(topUrl);
		arc.setTitle(title);
		arc.setTextSource(textSource);
		arc.setIsTop(isTop);
		arc.setNum(num);
		
		
        /*
         * 
         * 往后需添加一系列修改规则
         * 
         */
        
        
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        arc.setUpdateUser(sysUser.getId());
        arc.setUpdateTime(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));

        int flag = articleService.updateLocal(arc);
        //updateRedisUser();
        return new ResultObj(0, "修改成功", flag);
	}
	
	@RequestMapping(value = "/save/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable String id){
		WherePrams wherePrams=Method.where("isDelete",C.EQ,0);
		wherePrams.and("id", C.EQ, id);
		
		Article arc=articleService.get(wherePrams);
        arc.setIsDelete(1);

        int flag = articleService.updateLocal(arc);
        //updateRedisUser();
        return new ResultObj(0, "修改成功", flag);
	}
	
	
	
	@ResponseBody
	@RequestMapping("/search/{keyword}")
	public Object search(@PathVariable String keyword){
		WherePrams wherePrams=Method.where("isDelete",C.EQ,0);
		wherePrams.and("title", C.LIKE, keyword);
		
		PageUtil<Article> pageUtil = articleService.listPageCount(wherePrams);
        List<Article> articles = pageUtil.getData();
		
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Article article : articles) {
            Map<String, Object> articleMap = MapUtils.transBean2Map(article);
            maps.add(articleMap);
        }
        
        PageUtil<Map<String, Object>> pages = new PageUtil<>();

        CloneClass.getClone(pageUtil, pages);
        pages.setData(maps);
        return this.packForBT(pages);
	}
}













